package com.drcoffee.drcoffeemall.service.impl;

import com.drcoffee.drcoffeemall.entry.Attachment;
import com.drcoffee.drcoffeemall.entry.longRange.PartReq;
import com.drcoffee.drcoffeemall.mapper.LongRangeMapper;
import com.drcoffee.drcoffeemall.service.LongRangeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class LongRangeServiceImpl implements LongRangeService {

    @Autowired
    LongRangeMapper longRangeMapper;

    @Override
    public ResponseEntity<PageInfo<Attachment>> getAttachment(PartReq partReq) {
        String type_sql = "";
        if (Objects.equals(partReq.getPartType(), "P")) {
            type_sql = "AND B.part_code like '7%'";
        } else if (Objects.equals(partReq.getPartType(), "B")) {
            type_sql = "AND B.part_code like '8%'";
        } else if (Objects.equals(partReq.getPartType(), "N")) {
            type_sql = "";
        }
        PageHelper.startPage(partReq.getCurrentPage(), partReq.getPageSize());
        List<Attachment> attachmentList = null;
        attachmentList = longRangeMapper.getAttachment(partReq.getMachineId(), type_sql, "%" + partReq.getSearch() + "%");
        PageInfo<Attachment> attachmentPage = new PageInfo<>(attachmentList);
        return new ResponseEntity<>(attachmentPage, HttpStatus.OK);
    }
}
